<?php


namespace App\Web\Model;

use EchoPHP\Facades\Db;
use EchoPHP\Facades\Validation;
use EchoPHP\Validator\Validator;
use Ramsey\Uuid\Uuid;
use App\Web\Model\Project as ProjectModel;

class InterviewDetail extends Model{
    /**
     * 查询总数
     * @param array $search
     * @param $field
     * @param int $rowFrom
     * @param int $rowTo
     * @return array
     */
    public function getCount($oprId,$search){
        $field = 'count(1) as count';
        return $this->selDetail($oprId,$search, $field)[0]['count'];
    }
    /**
     * 查询面试通知
     * @param array $search
     * @param $field
     * @param int $rowFrom
     * @param int $rowTo
     * @return array
     */
    public function selDetail($oprId,$search = [], $field,$rowFrom = 0, $rowTo = 0){
        $return = [];
        if(empty($field)){
            $field  = 'id,resumeid,subid,projectid,caseid,tagcode,creator,createdate,updatedate,sendtype,status,content,senddate,realsenddate,batchcode,emailresult,emailfailreason,smsresult,smsfailreason,email,mobilephone,creatorname,sendbysys,name';
        }
        $sql = 'SELECT ' . $field . ' FROM InterviewDetail';
        $var = [];
        $where = [];
        $where[] = 'creator = ?';
        $var[] = $oprId;
        if (isset($search['batchcode']) && !empty($search['batchcode'])) {
           $where[] = 'batchcode = ?';
           $var[] = $search['batchcode'];
        }
        $sql .= ' WHERE ' . implode(' AND ', $where);
        $sql .= ' ORDER BY id DESC';
        $detailinfo = Db::select($sql, $var, $rowFrom, $rowTo);
        //查全部
        if($rowFrom==0 && $rowTo==0){
            return $detailinfo;
        }
        $return['interviewdetailinfo'] = $detailinfo;
        if(!$detailinfo){
            return $return;
        }
        $resumeIds = $projectids =$caseIds = [];
        foreach ($detailinfo as $item) {
            $caseIds[] = $item['caseid'];
            $projectids[] = $item['projectid'];
            $resumeIds[] = $item['resumeid'];
        }
        $caseIds = array_unique($caseIds);
        $projectids = array_unique($projectids);
        $resumeIds = array_unique($resumeIds);
        $managerModel = new Manager();
        $return['resumelist'] = $managerModel->getManagerListWithChkDownload($oprId, $resumeIds);
        $caseModel = new CaseModel();
        $return['caselist'] = $caseModel->getCaseList($caseIds, 'caseid,casename,subid,projectid,status');
        $projectModel = new ProjectModel();
        $return['projectlist'] = $projectModel->getProjectListById($projectids);
        return $return;
    }
}